package com.javalong.affairs.module.userModule.activity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.javalong.affairs.R;
import com.javalong.affairs.base.activity.UIHandlerActivity;
import com.javalong.affairs.base.constant.BundleKey;
import com.javalong.affairs.base.constant.RequestKey;
import com.javalong.affairs.base.data.AllData;
import com.javalong.affairs.base.listener.BaseResponseListener;
import com.javalong.affairs.base.model.Dept;
import com.javalong.affairs.base.model.User;
import com.javalong.affairs.base.ui.TextSelectView;
import com.javalong.affairs.base.ui.TopTitle;
import com.javalong.affairs.base.utils.HttpUtils;
import com.javalong.affairs.base.utils.UrlUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

@ContentView(R.layout.activity_updateuser)
public class UpdateUserInfoActivity extends UIHandlerActivity {

    @ViewInject(R.id.ui_toptitle)
    private TopTitle topTitle;
    @ViewInject(R.id.et_username)
    private EditText et_username;
    @ViewInject(R.id.et_workid)
    private EditText et_workid;
    @ViewInject(R.id.et_phone)
    private EditText et_phone;
    @ViewInject(R.id.ui_tsview_dept)
    private TextSelectView tsView_dept;
    @ViewInject(R.id.bt_update)
    private Button bt_update;
    @ViewInject(R.id.tv_dept)
    private TextView tv_dept;
    private User user;
    private Dept dept;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewUtils.inject(this);
        initData();
        initUI();
    }

    private void initUI() {
        bt_update.setOnClickListener(this);
        topTitle.setOnLeftDrawableClick(this);
        et_phone.setText(user.phone);
        et_username.setText(user.name);
        et_workid.setText(user.workId);
        tv_dept.setText(user.sectionName);
    }

    private void initData() {
        user = (User) AllData.GetInstance()
                .get(R.string.data_user_current_user);
    }

    @Override
    protected void onActivityResult(int arg0, int arg1, Intent arg2) {
        super.onActivityResult(arg0, arg1, arg2);
        if (arg0 != RESULT_OK) {
            return;
        }
        switch (arg1) {
            case 0:
                List<Dept> deptList = (List<Dept>) arg2
                        .getSerializableExtra(BundleKey.DEPTLIST);
                if (deptList != null && deptList.size() > 0) {
                    dept = deptList.get(0);
                }
                break;
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.bt_update:
                user.workId = et_workid.getText().toString();
                user.name = et_username.getText().toString();
                user.phone = et_phone.getText().toString();
                String url = UrlUtils.getUrl("User", "setDetail");
                Map<String, Object> params = new HashMap<String, Object>();
                params.put(RequestKey.WORKID, et_workid.getText().toString());
                params.put(RequestKey.NAME, et_username.getText().toString());
                params.put(RequestKey.PHONE, et_phone.getText().toString());
                if (dept == null) {
                    params.put(RequestKey.DEPTID, user.section);
                } else {
                    params.put(RequestKey.DEPTID, dept.id);
                }
                progressDialog.show();
                HttpUtils.post(url, params, new BaseResponseListener(
                        new TypeToken<User>() {
                        }.getType()) {
                    @Override
                    public void onSuccess(Object obj) {
                        super.onSuccess(obj);
                        progressDialog.dismiss();
                        finish();
                    }
                });
                break;
            default:
                break;
        }
    }
}
